package cn.onesdream.app.api.service.Impl;

import cn.onesdream.app.api.service.StarService;
import cn.onesdream.app.common.exception.ServiceException;
import cn.onesdream.app.common.util.ErrorType;
import cn.onesdream.app.dao.entity.Star;
import cn.onesdream.app.dao.entity.StarType;
import cn.onesdream.app.dao.entity.User;
import cn.onesdream.app.dao.mapper.StarMapper;
import cn.onesdream.app.dao.mapper.StarTypeMapper;
import cn.onesdream.app.dao.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author WangShiyu
 * @date 2021/3/9 11:31
 */
@Service
public class StarServiceImpl extends ServiceImpl<StarMapper, Star> implements StarService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StarTypeMapper starTypeMapper;

    @Override
    public void addDailyStar(Integer userId, Byte starTypeId) throws ServiceException {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        QueryWrapper<Star> starQueryWrapper = new QueryWrapper<>();
        starQueryWrapper.eq("user_id",userId)
                .eq("star_id",starTypeId)
                .likeRight("time",date.format(formatter));
        Star star = baseMapper.selectOne(starQueryWrapper);
        if(star == null){
            addStar(userId,starTypeId);
        }
        else{
            throw new ServiceException("该每日事件今日已使用");
        }
    }

    @Override
    public void updateUserStarNumber(Integer userId, Byte starTypeId) throws ServiceException {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        User user = userMapper.selectById(userId);
        StarType starType = starTypeMapper.selectById(starTypeId);
        Integer starNumber = user.getStarNumber();
        Byte number = starType.getNumber();
        if(starNumber + number >= 0){
            user.setStarNumber(starNumber + number);
            userUpdateWrapper.lambda()
                    .eq(User::getId, userId);
            userMapper.update(user,userUpdateWrapper);
        }
        else {
            throw new ServiceException("小星星余额不足！");
        }
    }

    @Override
    public void addStar(Integer userId, Byte starTypeId) throws ServiceException {
        updateUserStarNumber(userId, starTypeId);
        Star star = new Star();
        star.setUserId(userId);
        star.setStarId(starTypeId);
        baseMapper.insert(star);
    }

    @Override
    public StarType getStarType(Byte starTypeId) {
        return starTypeMapper.selectById(starTypeId);
    }

    @Override
    public List<Star> getStar(Integer userId) {
        QueryWrapper<Star> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return baseMapper.selectList(wrapper);
    }


}
